package cn.a592n.weightlibs.weight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Dizner on 2017/8/29.
 */

public class M2PullView extends View {


    private Path mPath;
    private Paint mPaint;
    private Path mPath2;


    public M2PullView(Context context) {
        super(context);
        new Thread(){
            @Override
            public void run() {
//                super.run();
                init();
            }
        }.start();
    }

    public M2PullView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        new Thread(){
            @Override
            public void run() {
//                super.run();
                init();
            }
        }.start();
    }

    public M2PullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        new Thread(){
            @Override
            public void run() {
//                super.run();
                init();
            }
        }.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public M2PullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        new Thread(){
            @Override
            public void run() {
//                super.run();
                init();
            }
        }.start();

    }

    private void init() {
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPath = new Path();
        mPath2 = new Path();
//        mPath.quadTo(200, 100, 300, 400);
//        mPath.rQuadTo(200, 100, 300, 400);
        mPath.cubicTo(200, 100, 300, 1000, 1300, 200);
        float[] xPoint = new float[]{0, 200, 300,1300};
        float[] yPoint = new float[]{0, 100, 1000,200};

        int fps = 10000;


        for (int i = 0; i <= fps; i++) {
            float pro = i / (float) fps;
            float point[] = jisuanPoint(pro, xPoint, yPoint);
            mPath2.lineTo(point[0], point[1]);
            postInvalidate();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }

    //
    private float[] jisuanPoint(float t, float[] xPoint, float[] yPoint) {
        float[] point = new float[2];
        int length = Math.min(xPoint.length, yPoint.length);
        for (int i = 1; i < length; i++) {

            for (int j = 0; j < length - i; j++) {

                //算法1
//                xPoint[j] = (1 - t) * xPoint[j] + t * xPoint[j + 1];
//                yPoint[j] = (1 - t) * yPoint[j] + t * yPoint[j + 1];
                //算法2
                xPoint[j] = xPoint[j] + (xPoint[j + 1] - xPoint[j]) * t;
                yPoint[j] = yPoint[j] + (yPoint[j + 1] - yPoint[j]) * t;
            }

        }
        point[0] = xPoint[0];
        point[1] = yPoint[0];
        return point;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.GREEN);
        canvas.drawPath(mPath, mPaint);
        mPaint.setColor(Color.RED);
        canvas.drawPath(mPath2, mPaint);
    }
}
